package com.it.base.arithmetic.base.dm.chain;

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements StudyPrepareFilter {
    private int pos = 0;
    private Study study;
    //存储这个链上所有需要执行的对象
    private List<StudyPrepareFilter> studyPrepareFilterList;

    public FilterChain(Study study) {
        this.study = study;
    }
    //在链上添加
    public void addFilter(StudyPrepareFilter studyPrepareFilter) {
        if (studyPrepareFilterList == null) {
            studyPrepareFilterList = new ArrayList<>();
        }
        studyPrepareFilterList.add(studyPrepareFilter);
    }

    @Override
    public void doFilter(PreparationList preparationList, FilterChain filterChain) {
        //最后一个对象了,需要执行
        if (pos == studyPrepareFilterList.size()) {
            study.study();
        }
        //通过一个下标指针来获取下一下需要执行的对象
        StudyPrepareFilter studyPrepareFilter =studyPrepareFilterList.get(pos++);
        if(studyPrepareFilter!=null){
            studyPrepareFilter.doFilter(preparationList, filterChain);
        }

    }
}
